package com.example.culturapp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ItemAdapter extends BaseExpandableListAdapter {
	private final SparseArray<GrupoDeItems> grupos;
	public LayoutInflater inflater;
	public Context activity;
	public TextView tvContenido;
	public RatingBar rbRating;
	public Button btnValorar;
	public Model md;

	public ItemAdapter(Context act, TextView tvCont, RatingBar rating, Button btnValorar,Model model) {
		
		activity = act;
		this.grupos = model.getListaRummores();
		this.tvContenido= tvCont;
		this.rbRating= rating;
		this.btnValorar= btnValorar;
		md=model;
		inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//.getLayoutInflater();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return grupos.get(groupPosition).children.get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		final String children = (String) getChild(groupPosition, childPosition);
		TextView textvw = null;
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.subitems_layout, null);
		}
		textvw = (TextView) convertView.findViewById(R.id.textView1);
		textvw.setText(children);
		switch (groupPosition){
		case 0:
			switch (childPosition){
			case 0:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.julia, 0, 0, 0);
				break;
			case 1:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.leila, 0, 0, 0);
				break;
			}
			break;
		case 1:
			switch (childPosition){
			case 0:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.brian, 0, 0, 0);
				break;
			case 1:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.george, 0, 0, 0);
				break;
			case 2:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lorena, 0, 0, 0);
				break;
			}
			break;
		case 2:
			switch (childPosition){
			case 0:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.cesar, 0, 0, 0);
				break;
			case 1:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.george, 0, 0, 0);
				break;
			case 2:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lorena, 0, 0, 0);
				break;
			case 3:
				textvw.setCompoundDrawablesWithIntrinsicBounds(R.drawable.luis, 0, 0, 0);
				break;
			}
			break;
			
		}
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MostrarEstadistica(groupPosition, childPosition);
			}
		});
		
		btnValorar.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mtdValorar(groupPosition, childPosition);
			}
			
		});
		
		return convertView;
	}
	
	public void mtdValorar(int groupPosition, int childPosition ){
		List<ItemRumor> lista= md.getListaRumoresEst();
		
		int indice= 2*groupPosition+ childPosition;
		ItemRumor item= (ItemRumor) lista.get(indice);
	
		btnValorar.setEnabled(false);
		rbRating.setRating(md.getEstadistica(item));
		rbRating.setEnabled(false);
		item.setVoto(true);
		lista.set(indice, item);
	}
	
	public void MostrarEstadistica(int groupPosition, int childPosition){
		List<ItemRumor> lista= md.getListaRumoresEst();
		
		int indice= 2*groupPosition+ childPosition;
		if(indice< lista.size()){
			ItemRumor item= (ItemRumor) lista.get(indice);
			tvContenido.setText(item.getContenido());
		
			if(item.isVoto()){
				btnValorar.setEnabled(false);
				rbRating.setRating(md.getEstadistica(item));
				rbRating.setEnabled(false);
				
			} else {
				btnValorar.setEnabled(true);
				rbRating.setEnabled(true);				
			}
				
		}
				
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		return grupos.get(groupPosition).children.size();
	}

	public Object getGroup(int groupPosition) {
		return grupos.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return grupos.size();
	}

	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.items_layout, null);
		}
		GrupoDeItems grupo = (GrupoDeItems) getGroup(groupPosition);
		((CheckedTextView) convertView).setText(grupo.string);
		((CheckedTextView) convertView).setChecked(isExpanded);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
}
